package accounts;

import exceptions.NotEnoughMoneyException;

/**
 * the checking account
 * @author Mike
 */

public class CheckingAccount extends Account {
    //no access modifier = access within the package
    int balance;
    private int limit;

    /**
     * create a new checking account
     * @param _bank the id of the bank it belongs to
     * @param _id the account numer
     * @param _owner the customer who owns the accoun
     * @param _balance the balance of the account
     * @param _limit the limit of the account
     */
    public CheckingAccount (int _bank,int _id, int _owner, int _balance, int _limit) {
        super(_bank,_id,_owner);
        balance = _balance;
        limit = _limit;
    }
    /**
     * remove a given amount of money from the account
     * @param amount the amount to withdraw
     * @throws NotEnoughMoneyException is thrown when not enough money is on the account to perform the action
     */
    public void withdraw (int amount) throws NotEnoughMoneyException {
        if (balance-amount < -limit) {
            throw new NotEnoughMoneyException(this);
        }
        Transaction.getBuilder().setType(TType.withdrawl).setAccounts(this,null).setAmount(amount).build();
    }
    /**
     * put a given amount of money inside the account
     * @param amount the amount of mouney to deposit
     */
    public void deposit (int amount) {
        Transaction.getBuilder().setType(TType.deposit).setAccounts(null, this).setAmount(amount).build();
    }
    /**
     * make a transfer of money from your account to another account
     * @param to the account to transfer the money to
     * @param amount the amount of money to transfer
     * @throws NotEnoughMoneyException is thrown when not enough money is on the account to perform the action
     */
    public void transferTo(Account to,int amount) throws NotEnoughMoneyException {
        if (balance-amount < -limit) {
            throw new NotEnoughMoneyException(this);
        }
        Transaction.getBuilder().setType(TType.transfer).setAccounts(this, to).setAmount(amount).build();
    }
    /**
     * returns the balance of the account
     * @return the balance
     */
    @Override
    public int getBalance () {
        return balance;
    }
}